layui.config({
	base: '/static/plugin/build/js/'
}).use(['table', 'form', 'layer', 'ztree'], function() {
	var $ = layui.$;
	var table = layui.table;
	var form = layui.form;
	table.render({
		elem: '#recordResult', //指定原始表格元素选择器（推荐id选择器）
		url: '/mock/recordResult.json', //请求地址
		cols: [
			[{
					checkbox: true,
					fixed: true
				},
				{
					field: 'cname',
					title: '部门',
					width: '20%'
				},
				{
					field: 'standardRate',
					title: '规范率',
					width: '15%'
				},
				{
					field: 'timelyRate',
					title: '报备及时率',
					width: '11%'
				},
				{
					field: 'suspendRate',
					title: '暂缓登记率',
					width: '10%'
				},
				{
					field: 'secondaryRate',
					title: '纠正率',
					width: '20%'
				},
				{
					fixed: 'right',
					title: '报送总数',
					width: '20%',
					field: 'total'
				}
			]
		], //设置表头
		id: 'table_reload', //设定容器唯一ID
		page: false, //开启分页
		limits: [10, 20, 50, 100], //每页数量
		limit: 10, //默认每页数量
		height: 'full-90', //容器高度
		method: 'get',
		even: true //隔行换色
	});
	initDeptTree();
	//加载部门zTree
	function initDeptTree() {
		$.get("/mock/deptztree.json", function(result) {
			if(result.code == "0") {
				//初始化部门树
				var setting = {
					data: {
						simpleData: {
							enable: true
						}
					},
					callback: {
						onCheck: onCheck
					},
					check: {
						enable: true,
						chkboxType: {
							"Y": "s",
							"N": "s"
						}
					},
					view: {
						dblClickExpand: false,
						showLine: true,
						selectedMulti: false
					}
				};
				var zNodes = result.data;
				$.fn.zTree.init($("#ztree"), setting, zNodes);
			} else {
				top.layer.msg(result.msg);
			}
		}, "json");
	}

	//点选树节点回显至输入框
	function onCheck(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("ztree");
		var nodes = zTree.getCheckedNodes(true);
		var deptName = "";
		var deptID = "";
		for(var i = 0, l = nodes.length; i < l; i++) {
			deptName += nodes[i].name + ",";
			deptID += nodes[i].id + ",";
		}
		if(deptName.length > 0) deptName = deptName.substring(0, deptName.length - 1);
		if(deptID.length > 0) deptID = deptID.substring(0, deptID.length - 1);
		var deptNameObj = $("#deptName");
		deptNameObj.val(deptName);
		var deptIDObj = $("#deptID");
		deptIDObj.val(deptID);
	}
    $("#deptName").on('click',function(){
    	showMenu();
    })
	function showMenu() {
		var deptTreeObj = $("#deptName");
		var deptTreeWidth = $("#deptName").outerWidth(); // 适应宽度
		$("#menuContent").css("width", deptTreeWidth).slideDown("fast");

		$("body").bind("mousedown", onBodyDown);
	}

	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}

	function onBodyDown(event) {
		if(!(event.target.id == "m_cancel" || event.target.id == "m_set" || event.target.id == "rMenu" || event.target.id == "deptTreeBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
			hideMenu();
		}
	}

});